package com.example.bootcurriculumproject.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.bootcurriculumproject.entity.Ship;
import com.example.bootcurriculumproject.mapper.ShipMapper;
import com.example.bootcurriculumproject.service.ShipService;
import com.example.bootcurriculumproject.service.impl.ShipServiceImpl;
import com.example.bootcurriculumproject.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/ship")
public class ShipController {
    @Autowired
    ShipService shipService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ShipMapper shipMapper;
    /**
     * 显示所有地址信息
     * @return
     */
    @GetMapping("/selectAllShip")
    @ResponseBody
    @CrossOrigin
    public List<Ship> selectAll(){//http://localhost:8080/ship/selectAllShip
        LambdaQueryWrapper<Ship> queryWrapper = new LambdaQueryWrapper<>();
        String uid = String.valueOf(redisUtil.get("nowUid")) ;
        queryWrapper.eq(Ship::getUid,uid);
        return shipMapper.selectList(queryWrapper);

    }

    /**
     * 添加地址信息
     * @param ship
     * @return
     */
    @PostMapping("/addShip")
    @ResponseBody
    @CrossOrigin
    public boolean addShip(@RequestBody Ship ship){
        ship.setUid(String.valueOf(redisUtil.get("nowUid")));
        shipService.save(ship);
        return true;
    }

    /**
     * 根据地址编号删除地址信息
     * @param addressnum
     * @return
     */
    @DeleteMapping("/deleteShip/{addressnum}")
    @ResponseBody
    @CrossOrigin
    public boolean deleteShip(@PathVariable Long addressnum){
        shipService.deleteShip(addressnum);
        return true;
    }

    /**
     * 更新地址信息
     * @param ship
     * @return
     */
    @PutMapping("/updateShip")
    @ResponseBody
    @CrossOrigin
    public boolean updateUser(@RequestBody Ship ship){
        shipService.updateShip(ship);
        return true;
}

    /**
     * 根据地址编号查找地址信息
     * @param addressnum
     * @return
     */
    @GetMapping("/selectByAddressnum/{addressnum}")
    @ResponseBody
    @CrossOrigin
    public Ship selectByAddressnum(@PathVariable Long addressnum){
        Ship ship=shipService.selectByAddressnum(addressnum);
        return  ship;
    }
}
